package cn.edu.nju.ws.sview.wyge.servlets;

import java.io.IOException;
import java.net.URLDecoder;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.SQLException;
import java.util.ArrayList;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.json.simple.JSONArray;
import org.json.simple.JSONObject;
import org.json.simple.JSONValue;

import cn.edu.nju.ws.sview.database.DBConnectionFactory;
import cn.edu.nju.ws.sview.views.User;
import cn.edu.nju.ws.sview.wyge.relations.adataset.bookmark.DatasetBookmark;
import cn.edu.nju.ws.sview.wyge.relations.datasets.DatasetItem;

public class DsBookmarkManageServlet extends HttpServlet {

	/**
	 * The doGet method of the servlet. <br>
	 *
	 * This method is called when a form has its tag value method equals to get.
	 * 
	 * @param request the request send by the client to the server
	 * @param response the response send by the server to the client
	 * @throws ServletException if an error occurred
	 * @throws IOException if an error occurred
	 */
	public void doGet(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {
		this.doPost(request, response);
	}

	/**
	 * The doPost method of the servlet. <br>
	 *
	 * This method is called when a form has its tag value method equals to post.
	 * 
	 * @param request the request send by the client to the server
	 * @param response the response send by the server to the client
	 * @throws ServletException if an error occurred
	 * @throws IOException if an error occurred
	 */
	public void doPost(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {
		response.setContentType("text/xml;charset=utf-8");
		String json = request.getParameter("msg");
		if(json != null){
			String data = URLDecoder.decode(json, "utf8");
			JSONObject regtJSON = (JSONObject) JSONValue.parse(data);
			String action = (String)regtJSON.get("action");
			if(action.contentEquals("getDatasetbmList")){//for bm_manage.jsp
				String uname = (String) regtJSON.get("user");
				String lang = (String) regtJSON.get("lang");
				ArrayList<Integer> bmList = DatasetBookmark.getBookmarkIdsByUserId(User.getUserIdByName(uname));
				JSONArray elements = new JSONArray();
				for(Integer bmId : bmList){
					JSONObject jsob = new JSONObject();
					jsob.put("bmid", bmId);
					jsob.put("bmname", DatasetBookmark.getBookmarkNameById(bmId));
					DatasetItem ds = DatasetBookmark.getDatasetItemOfBookmark(bmId);
					if(ds==null) {
						System.out.println("error in DsBookmarkManageServlet!! bmId="+bmId);
						continue;
					}
//					System.out.println("bm:"+bmId+"|"+ds+"|");
					jsob.put("ds_type", ds.getGenre());
					jsob.put("dsid", ds.getId());
					jsob.put("dsname", DatasetItem.getShowNameByDatasetID(ds, lang));//======ds.getName()
					elements.add(jsob);
				}
				
				JSONObject result = new JSONObject();
				result.put("result", elements);
				response.getWriter().print(result);
			}else if(action.contentEquals("delEBM")){
				String result = null;
				String bmIds = (String) regtJSON.get("bmIds");
				try {
					Connection conn = DBConnectionFactory.getConnection();
					PreparedStatement pstmt = conn.prepareStatement("delete from ds_bookmark,ds_bookmark_dataset using ds_bookmark,ds_bookmark_dataset where id in ("+bmIds+") and ds_bookmark.id=ds_bookmark_dataset.ds_bm_id;");
					pstmt.executeUpdate();
					pstmt.close();
					conn.close();
					result = "success";
				} catch (SQLException e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
				}
				response.getWriter().print(result);
			}
		}
	}

}
